﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.business
{
    public partial class app_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            action = DTRequest.GetQueryString("action");
            id = DTRequest.GetQueryInt("id");
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("app_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }


        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.sf_appidmanage bll = new BLL.sf_appidmanage();
            Model.sf_appidmanage model = bll.GetModel(_id);

            txtappid_name.Text = model.appid_name;
            txtappid_origin_id.Text = model.appid_origin_id;
            txtweixin_account.Text = model.weixin_account;
            txtavatar.Text = model.avatar;
            txtinterface_url.Text = model.interface_url;
            txttoken_value.Text = model.token_value;
            txtencodingaeskey.Text = model.encodingaeskey;
            txtappid.Text = model.appid;
            txtappsecret.Text = model.appsecret;
            
            txtpayment_name.Text = model.payment_name;
            cbstate.Checked = model.state == 1 ? true : false;
            txtweixin_pay_account.Text = model.weixin_pay_account;
            txtaccount_pay_key.Text = model.account_pay_key;
            rblsend_type.SelectedValue = model.send_type.ToString();
            txtlogo.Text = model.logo;
            txtdescription.Text = model.description; 
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            BLL.sf_appidmanage bll = new BLL.sf_appidmanage();
            Model.sf_appidmanage model = new Model.sf_appidmanage();
            Model.manager model1 = (Model.manager)Session[DTKeys.SESSION_ADMIN_INFO];

            model.businessNum = model1.busnieseNum;
            model.appid_name = txtappid_name.Text.Trim();
            model.appid_origin_id = txtappid_origin_id.Text.Trim();
            model.weixin_account = txtweixin_account.Text.Trim();
            model.avatar = txtavatar.Text;
            model.interface_url = txtinterface_url.Text.Trim();
            model.token_value = txttoken_value.Text.Trim();
            model.encodingaeskey = txtencodingaeskey.Text.Trim();
            model.appid = txtappid.Text.Trim().Trim();
            model.appsecret = txtappsecret.Text.Trim();

            model.payment_name = txtpayment_name.Text;
            model.state = cbstate.Checked ? 1 : 0;
            model.weixin_pay_account = txtweixin_pay_account.Text.Trim();
            model.account_pay_key = txtaccount_pay_key.Text.Trim();
            model.send_type = int.Parse(rblsend_type.SelectedValue);
            model.logo = txtlogo.Text;
            model.description = txtdescription.Text;

            model.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            model.create_user = model1.real_name;

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加公众服务号:" + model.appid_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            //调整AppId时，需要调整的表
            string [] strTables = null;
            BLL.sf_appidmanage bll = new BLL.sf_appidmanage();
            Model.sf_appidmanage model = bll.GetModel(_id);

            if (txtappid.Text.Trim().Equals(model.appid) == false) 
            {
                strTables = new string[] { "sf_contract_child", "sf_user_info", "sf_goods_info", "sf_goods_amounts", "sf_goods_cart", "sf_goods_color", "sf_goods_img", "sf_goods_index_pics", "sf_goods_order", "sf_goods_orderdetal", "sf_goods_size", "sf_goods_types", "sf_user_address", "sf_user_bank", "sf_user_withdraw_order", "sf_auto_reply" };
            }

            model.id = _id; 
            model.appid_name = txtappid_name.Text;
            model.appid_origin_id = txtappid_origin_id.Text;
            model.weixin_account = txtweixin_account.Text;
            model.avatar = txtavatar.Text;
            model.interface_url = txtinterface_url.Text;
            model.token_value = txttoken_value.Text;
            model.encodingaeskey = txtencodingaeskey.Text;
            model.appid = txtappid.Text.Trim();
            model.appsecret = txtappsecret.Text;

            model.payment_name = txtpayment_name.Text;
            model.state = cbstate.Checked ? 1 : 0;
            model.weixin_pay_account = txtweixin_pay_account.Text;
            model.account_pay_key = txtaccount_pay_key.Text;
            model.send_type = int.Parse(rblsend_type.SelectedValue);
            model.logo = txtlogo.Text;
            model.description = txtdescription.Text;  

            if (bll.Update(model,strTables) == true)
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改公众服务号:" + model.appid_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //验证appid是否重复
            bool repeat = false;
            BLL.sf_appidmanage bll = new BLL.sf_appidmanage();

            if (action == DTEnums.ActionEnum.Add.ToString())
            {
                System.Data.DataSet ds = bll.GetList(" appid = '" + txtappid.Text.Trim() + "' ");
                if(ds.Tables[0].Rows.Count >  0)
                {
                    repeat = true;
                }
            }
            else if (action == DTEnums.ActionEnum.Edit.ToString()) 
            {
                System.Data.DataSet ds = bll.GetList(" id <> " + this.id + " and appid = '" + txtappid.Text.Trim() + "' ");
                if(ds.Tables[0].Rows.Count > 0)
                {
                    repeat = true;
                }
            }

            if (repeat == true)
            {
                JscriptMsg("提示：AppId在系统中重复,<br>请检查您的AppId或者联系官方系统管理员！", string.Empty);
                return;
            }

            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                //ChkAdminLevel("app_edit", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦！", string.Empty);
                    return;
                }
                JscriptMsg("修改信息成功！", "app_list.aspx");
            }
            else //添加
            {
                //ChkAdminLevel("app_edit", DTEnums.ActionEnum.Add.ToString()); //检查权限 

                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加信息成功！", "app_list.aspx");
            }
        }
    }
}